package com.ysd.lis.job;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.lis.entity.eq.*;
import com.ysd.lis.mapper.eq.*;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.eq.*;
import com.ysd.util.ToolsUtils;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component("jobManage")
public class JobManage {
    @Autowired
    EqMaintainPlanMapper eqMaintainPlanMapper;
    @Autowired
    EqMaintainPlanPersonMapper eqMaintainPlanPersonMapper;
    @Autowired
    EqMaintainPlanDetailMapper eqMaintainPlanDetailMapper;
    @Autowired
    SysRuleSetService sysRuleSetService;//编码规则服务
    @Autowired
    EqMaintainPlanService eqMaintainPlanService;
    @Autowired
    EqMaintainOrderService eqMaintainOrderService;
    @Autowired
    EqMaintainPersonService eqMaintainPersonService;
    @Autowired
    EqMaintainDetailService eqMaintainDetailService;
    @Autowired
    EqPatrolPlanMapper eqPatrolPlanMapper;
    @Autowired
    EqPatrolPlanService eqPatrolPlanService;
    @Autowired
    private EqPatrolPlanEqMapper eqPatrolPlanEqMapper;
    @Autowired
    private EqPatrolPlanEqStMapper eqPatrolPlanEqStMapper;
    @Autowired
    private EqPatrolPlanEqStItService eqPatrolPlanEqStItService;
    @Autowired
    private EqPatrolTaskMapper eqPatrolTaskMapper;
    @Autowired
    private EqPatrolTaskEqMapper eqPatrolTaskEqMapper;
    @Autowired
    private EqPatrolTaskEqStMapper eqPatrolTaskEqStMapper;
    @Autowired
    private EqPatrolTaskEqStItMapper eqPatrolTaskEqStItMapper;
    @Autowired
    EqSpotcheckPlanMapper eqSpotcheckPlanMapper;
    @Autowired
    EqSpotcheckPlanService eqSpotcheckPlanService;

    @Autowired
    EqSpotcheckTaskMapper eqSpotcheckTaskMapper;
    @Autowired
    EqSpotcheckPlanEqMapper eqSpotcheckPlanEqMapper;
    @Autowired
    EqSpotcheckPlanEqStMapper eqSpotcheckPlanEqStMapper;
    @Autowired
    EqSpotcheckTaskEqMapper eqSpotcheckTaskEqMapper;

    @Autowired
    private EqSpotcheckPlanEqStItService eqSpotcheckPlanEqStItService;
    @Autowired
    private EqSpotcheckTaskEqStMapper eqSpotcheckTaskEqStMapper;

    @Autowired
    private EqSpotcheckTaskEqStItMapper eqSpotcheckTaskEqStItMapper;
    public void execEqMaintainPlanJob() {
        System.out.println("execEqMaintainPlanJob=======================:" + new Date());
        LambdaQueryWrapper<EqMaintainPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String[] statuss = new String[]{"5", "10"};
        lambdaQueryWrapper.le(EqMaintainPlan::getNextDate, format)
                .in(EqMaintainPlan::getPlanStatus, statuss).eq(EqMaintainPlan::getDelFlag, 0);
        List<EqMaintainPlan> planList = eqMaintainPlanMapper.selectList(lambdaQueryWrapper);
        if (ToolsUtils.isNotEmpty(planList)) {
            for (EqMaintainPlan plan : planList) {
                if ("2".equals(plan.getPlanType())) {
                    CronExpression cronExpression;
                    try {
                        cronExpression = new CronExpression(plan.getCron());
                        Date nextInvalidTimeAfter = cronExpression.getNextValidTimeAfter(new Date());
                        if (DateUtil.compare(nextInvalidTimeAfter, plan.getEndDate()) > 0) {
                            plan.setPlanStatus("99");
                        } else {
                            plan.setPlanStatus("10");
                            plan.setNextDate(new Timestamp(nextInvalidTimeAfter.getTime()));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    plan.setPlanStatus("99");
                }
            }
            eqMaintainPlanService.updateBatchById(planList);
            //根据planId查出所有的计划信息
            List<String> planIds = planList.stream().map(EqMaintainPlan::getId).collect(Collectors.toList());

            //巡检计划人
            LambdaQueryWrapper<EqMaintainPlanPerson> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EqMaintainPlanPerson::getDelFlag, 0).in(EqMaintainPlanPerson::getPlanId, planIds);
            List<EqMaintainPlanPerson> maintainPlanPersonList = eqMaintainPlanPersonMapper.selectList(queryWrapper);

            //巡检计划设备的巡检标准
            LambdaQueryWrapper<EqMaintainPlanDetail> queryWrapperSt = new LambdaQueryWrapper<>();
            queryWrapperSt.eq(EqMaintainPlanDetail::getDelFlag, 0).in(EqMaintainPlanDetail::getPlanId, planIds);
            List<EqMaintainPlanDetail> maintainPlanDetailList = eqMaintainPlanDetailMapper.selectList(queryWrapperSt);

            //生成任务数据
            //批量生成任务
            List<EqMaintainOrder> addMaintainOrderList = new ArrayList<>();
            for (EqMaintainPlan plan : planList) {
                EqMaintainOrder maintainOrder = new EqMaintainOrder();
                maintainOrder.setPlanId(plan.getId());
                maintainOrder.setPlanNo(plan.getPlanNo());
                String a = sysRuleSetService.createRuleCodeList("eqMaintainNo", 1).get(0);
                maintainOrder.setMaintainNo(a);
                maintainOrder.setEquiptId(plan.getEquiptId());
                maintainOrder.setEquiptCode(plan.getEquiptCode());
                maintainOrder.setMaintainType(plan.getMaintainType());
                maintainOrder.setMaintainStatus("0");
                maintainOrder.setPlanDate(plan.getNextDate());
                addMaintainOrderList.add(maintainOrder);
            }
            if (ToolsUtils.isNotEmpty(addMaintainOrderList)) {
                boolean b = eqMaintainOrderService.saveBatch(addMaintainOrderList);
                //批量生成任务下得保养人
                List<EqMaintainPerson> addEqMaintainPersonList = new ArrayList<>();
                for (EqMaintainPlanPerson planPerson : maintainPlanPersonList) {
                    Optional<EqMaintainOrder> first = addMaintainOrderList.stream().filter(t -> t.getPlanId().equals(planPerson.getPlanId())).findFirst();
                    if (first.isPresent()) {
                        EqMaintainOrder eqMaintainOrder = first.get();
                        EqMaintainPerson addMaintainPerson = new EqMaintainPerson();
                        addMaintainPerson.setMaintainId(eqMaintainOrder.getId());
                        addMaintainPerson.setMaintainNo(eqMaintainOrder.getMaintainNo());
                        addMaintainPerson.setPersonId(planPerson.getPersonId());
                        addMaintainPerson.setPerson(planPerson.getPerson());
                        addMaintainPerson.setPlanId(planPerson.getPlanId());
                        addMaintainPerson.setPlanPersonId(planPerson.getId());
                        addMaintainPerson.setSeq(planPerson.getSeq());
                        addEqMaintainPersonList.add(addMaintainPerson);
                    }
                }
                if (ToolsUtils.isNotEmpty(addEqMaintainPersonList)) {
                    eqMaintainPersonService.saveBatch(addEqMaintainPersonList);
                }
                //批量生成明细
                List<EqMaintainDetail> addEqMaintainDetailList = new ArrayList<>();
                for (EqMaintainPlanDetail planDetail : maintainPlanDetailList) {
                    Optional<EqMaintainOrder> first = addMaintainOrderList.stream().filter(t -> t.getPlanId().equals(planDetail.getPlanId())).findFirst();
                    if (first.isPresent()) {
                        EqMaintainOrder eqMaintainOrder = first.get();
                        EqMaintainDetail addMaintainDetail = new EqMaintainDetail();
                        addMaintainDetail.setMaintainId(eqMaintainOrder.getId());
                        addMaintainDetail.setMaintainNo(eqMaintainOrder.getMaintainNo());
                        addMaintainDetail.setStandardId(planDetail.getStandardId());
                        addMaintainDetail.setStandardName(planDetail.getStandardName());
                        addMaintainDetail.setStandardCode(planDetail.getStandardCode());
                        addMaintainDetail.setPosition(planDetail.getPosition());
                        addMaintainDetail.setStandard(planDetail.getStandard());
                        addMaintainDetail.setPlanId(planDetail.getPlanId());
                        addMaintainDetail.setPlanDetailId(planDetail.getId());
                        addMaintainDetail.setStatus(0);
                        addMaintainDetail.setSeq(planDetail.getSeq());
                        addEqMaintainDetailList.add(addMaintainDetail);
                    }
                }
                if (ToolsUtils.isNotEmpty(addEqMaintainDetailList)) {
                    eqMaintainDetailService.saveBatch(addEqMaintainDetailList);
                }

            }

        }

    }

    //巡检计划定时任务
    public void execEqPatrolPlanJob() {
        System.out.println("execEqPatrolPlanJob=======================:" + new Date());
        LambdaQueryWrapper<EqPatrolPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String[] statuss = new String[]{"5", "10"};
        lambdaQueryWrapper.le(EqPatrolPlan::getNextTime, format)
                .in(EqPatrolPlan::getStatus, statuss).eq(EqPatrolPlan::getDelFlag, 0);
        List<EqPatrolPlan> planList = eqPatrolPlanMapper.selectList(lambdaQueryWrapper);
        if (ToolsUtils.isNotEmpty(planList)) {
            for (EqPatrolPlan plan : planList) {
                if ("2".equals(plan.getPlanType())) {
                    CronExpression cronExpression;
                    try {
                        cronExpression = new CronExpression(plan.getCron());
                        Date nextInvalidTimeAfter = cronExpression.getNextValidTimeAfter(new Date());
                        if (DateUtil.compare(nextInvalidTimeAfter, plan.getEndTime()) > 0) {
                            plan.setStatus("99");
                        } else {
                            plan.setStatus("10");
                            plan.setNextTime(new Timestamp(nextInvalidTimeAfter.getTime()));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    plan.setStatus("99");
                }
            }
            eqPatrolPlanService.updateBatchById(planList);
            //根据planId查出所有的计划信息
            for (EqPatrolPlan eqPatrolPlan : planList) {
                String a = sysRuleSetService.createRuleCodeList("EqPatrolTaskCode", 1).get(0);
                //新增任务
                EqPatrolPlan patrolPlan = eqPatrolPlanMapper.selectById(eqPatrolPlan.getId());
                EqPatrolTask eqPatrolTask = new EqPatrolTask();
                BeanUtil.copyProperties(patrolPlan, eqPatrolTask);
                eqPatrolTask.setId(null);
                eqPatrolTask.setPlanId(patrolPlan.getId());
                eqPatrolTask.setCode(a);
                eqPatrolTask.setStatus("0");
                eqPatrolTask.setPlanDate(patrolPlan.getNextTime());
                eqPatrolTaskMapper.insert(eqPatrolTask);
                List<EqPatrolPlanEq> eqPatrolPlanEqs = eqPatrolPlanEqMapper.selectList(new LambdaQueryWrapper<EqPatrolPlanEq>().eq(EqPatrolPlanEq::getPlanId, eqPatrolPlan.getId()).eq(EqPatrolPlanEq::getDelFlag, 0));
                for (EqPatrolPlanEq eqPatrolPlanEq : eqPatrolPlanEqs) {
                    EqPatrolTaskEq eqPatrolTaskEq = new EqPatrolTaskEq();
                    BeanUtil.copyProperties(eqPatrolPlanEq, eqPatrolTaskEq);
                    eqPatrolTaskEq.setId(null);
                    eqPatrolTaskEq.setTaskId(eqPatrolTask.getId());
                    eqPatrolTaskEq.setPlanId(eqPatrolPlan.getId());
                    eqPatrolTaskEq.setPlanEqId(eqPatrolPlanEq.getId());
                    eqPatrolTaskEqMapper.insert(eqPatrolTaskEq);
                    List<EqPatrolPlanEqSt> eqPatrolPlanEqSts = eqPatrolPlanEqStMapper.selectList(new LambdaQueryWrapper<EqPatrolPlanEqSt>().eq(EqPatrolPlanEqSt::getPlanId, eqPatrolPlan.getId()).eq(EqPatrolPlanEqSt::getDelFlag, 0));
                    List<EqPatrolPlanEqStIt> eqPatrolPlanEqStIts = eqPatrolPlanEqStItService.list(new LambdaQueryWrapper<EqPatrolPlanEqStIt>().in(EqPatrolPlanEqStIt::getPlanId, eqPatrolPlan.getId()).eq(EqPatrolPlanEqStIt::getDelFlag, 0));

                    for (EqPatrolPlanEqSt eqPatrolPlanEqSt : eqPatrolPlanEqSts) {
                        EqPatrolTaskEqSt eqPatrolTaskEqSt = new EqPatrolTaskEqSt();
                        BeanUtil.copyProperties(eqPatrolPlanEqSt, eqPatrolTaskEqSt);
                        eqPatrolTaskEqSt.setId(null);
                        eqPatrolTaskEqSt.setTaskId(eqPatrolTask.getId());
                        eqPatrolTaskEqSt.setTaskEqId(eqPatrolTaskEq.getId());
                        eqPatrolTaskEqSt.setPlanId(eqPatrolPlan.getId());
                        eqPatrolTaskEqSt.setPlanEqId(eqPatrolPlanEq.getId());
                        eqPatrolTaskEqSt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                        eqPatrolTaskEqStMapper.insert(eqPatrolTaskEqSt);
                        List<EqPatrolPlanEqStIt> eqPatrolPlanEqStItList = eqPatrolPlanEqStIts.stream().filter(item -> item.getPlanEqStId().equals(eqPatrolPlanEqSt.getId())).collect(Collectors.toList());
                        for (EqPatrolPlanEqStIt eqPatrolPlanEqStIt : eqPatrolPlanEqStItList) {
                            EqPatrolTaskEqStIt eqPatrolTaskEqStIt = new EqPatrolTaskEqStIt();
                            BeanUtil.copyProperties(eqPatrolPlanEqStIt, eqPatrolTaskEqStIt);
                            eqPatrolTaskEqStIt.setId(null);
                            eqPatrolTaskEqStIt.setTaskId(eqPatrolTask.getId());
                            eqPatrolTaskEqStIt.setTaskEqId(eqPatrolTaskEq.getId());
                            eqPatrolTaskEqStIt.setTaskEqStId(eqPatrolTaskEqSt.getId());
                            eqPatrolTaskEqStIt.setPlanId(eqPatrolPlan.getId());
                            eqPatrolTaskEqStIt.setPlanEqId(eqPatrolPlanEq.getId());
                            eqPatrolTaskEqStIt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                            eqPatrolTaskEqStIt.setPlanEqStItId(eqPatrolPlanEqStIt.getId());
                            eqPatrolTaskEqStItMapper.insert(eqPatrolTaskEqStIt);
                        }
                    }
                }
            }
        }
    }

    //点检计划定时任务
    public void execEqSpotcheckPlanJob() {
        System.out.println("execEqSpotcheckPlanJob=======================:" + new Date());
        LambdaQueryWrapper<EqSpotcheckPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String format = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        String[] statuss = new String[]{"5", "10"};
        lambdaQueryWrapper.le(EqSpotcheckPlan::getNextTime, format)
                .in(EqSpotcheckPlan::getStatus, statuss).eq(EqSpotcheckPlan::getDelFlag, 0);
        List<EqSpotcheckPlan> planList = eqSpotcheckPlanMapper.selectList(lambdaQueryWrapper);
        if (ToolsUtils.isNotEmpty(planList)) {
            for (EqSpotcheckPlan plan : planList) {
                if ("2".equals(plan.getPlanType())) {
                    CronExpression cronExpression;
                    try {
                        cronExpression = new CronExpression(plan.getCron());
                        Date nextInvalidTimeAfter = cronExpression.getNextValidTimeAfter(new Date());
                        if (DateUtil.compare(nextInvalidTimeAfter, plan.getEndTime()) > 0) {
                            plan.setStatus("99");
                        } else {
                            plan.setStatus("10");
                            plan.setNextTime(new Timestamp(nextInvalidTimeAfter.getTime()));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    plan.setStatus("99");
                }
            }
            eqSpotcheckPlanService.updateBatchById(planList);
            //根据planId查出所有的计划信息
            for (EqSpotcheckPlan eqPatrolPlan : planList) {
                String a = sysRuleSetService.createRuleCodeList("EqSpotcheckPlanCode", 1).get(0);
                //新增任务
                EqSpotcheckPlan patrolPlan = eqSpotcheckPlanMapper.selectById(eqPatrolPlan.getId());
                EqSpotcheckTask eqPatrolTask = new EqSpotcheckTask();
                BeanUtil.copyProperties(patrolPlan, eqPatrolTask);
                eqPatrolTask.setId(null);
                eqPatrolTask.setPlanId(patrolPlan.getId());
                eqPatrolTask.setCode(a);
                eqPatrolTask.setStatus("0");
                eqPatrolTask.setPlanDate(patrolPlan.getNextTime());


                eqSpotcheckTaskMapper.insert(eqPatrolTask);
                List<EqSpotcheckPlanEq> eqPatrolPlanEqs = eqSpotcheckPlanEqMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEq>().eq(EqSpotcheckPlanEq::getPlanId, eqPatrolPlan.getId()).eq(EqSpotcheckPlanEq::getDelFlag, 0));
                for (EqSpotcheckPlanEq eqPatrolPlanEq : eqPatrolPlanEqs) {
                    EqSpotcheckTaskEq eqPatrolTaskEq = new EqSpotcheckTaskEq();
                    BeanUtil.copyProperties(eqPatrolPlanEq, eqPatrolTaskEq);
                    eqPatrolTaskEq.setId(null);
                    eqPatrolTaskEq.setTaskId(eqPatrolTask.getId());
                    eqPatrolTaskEq.setPlanId(eqPatrolPlan.getId());
                    eqPatrolTaskEq.setPlanEqId(eqPatrolPlanEq.getId());
                    eqSpotcheckTaskEqMapper.insert(eqPatrolTaskEq);
                    List<EqSpotcheckPlanEqSt> eqPatrolPlanEqSts = eqSpotcheckPlanEqStMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEqSt>().eq(EqSpotcheckPlanEqSt::getPlanId, eqPatrolPlan.getId()).eq(EqSpotcheckPlanEqSt::getDelFlag, 0));
                    List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStIts = eqSpotcheckPlanEqStItService.list(new LambdaQueryWrapper<EqSpotcheckPlanEqStIt>().in(EqSpotcheckPlanEqStIt::getPlanId, eqPatrolPlan.getId()).eq(EqSpotcheckPlanEqStIt::getDelFlag, 0));

                    for (EqSpotcheckPlanEqSt eqPatrolPlanEqSt : eqPatrolPlanEqSts) {
                        EqSpotcheckTaskEqSt eqPatrolTaskEqSt = new EqSpotcheckTaskEqSt();
                        BeanUtil.copyProperties(eqPatrolPlanEqSt, eqPatrolTaskEqSt);
                        eqPatrolTaskEqSt.setId(null);
                        eqPatrolTaskEqSt.setTaskId(eqPatrolTask.getId());
                        eqPatrolTaskEqSt.setTaskEqId(eqPatrolTaskEq.getId());
                        eqPatrolTaskEqSt.setPlanId(eqPatrolPlan.getId());
                        eqPatrolTaskEqSt.setPlanEqId(eqPatrolPlanEq.getId());
                        eqPatrolTaskEqSt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                        eqSpotcheckTaskEqStMapper.insert(eqPatrolTaskEqSt);
                        List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStItList = eqPatrolPlanEqStIts.stream().filter(item -> item.getPlanEqStId().equals(eqPatrolPlanEqSt.getId())).collect(Collectors.toList());
                        for (EqSpotcheckPlanEqStIt eqPatrolPlanEqStIt : eqPatrolPlanEqStItList) {
                            EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt = new EqSpotcheckTaskEqStIt();
                            BeanUtil.copyProperties(eqPatrolPlanEqStIt, eqPatrolTaskEqStIt);
                            eqPatrolTaskEqStIt.setId(null);
                            eqPatrolTaskEqStIt.setTaskId(eqPatrolTask.getId());
                            eqPatrolTaskEqStIt.setTaskEqId(eqPatrolTaskEq.getId());
                            eqPatrolTaskEqStIt.setTaskEqStId(eqPatrolTaskEqSt.getId());
                            eqPatrolTaskEqStIt.setPlanId(eqPatrolPlan.getId());
                            eqPatrolTaskEqStIt.setPlanEqId(eqPatrolPlanEq.getId());
                            eqPatrolTaskEqStIt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                            eqPatrolTaskEqStIt.setPlanEqStItId(eqPatrolPlanEqStIt.getId());
                            eqSpotcheckTaskEqStItMapper.insert(eqPatrolTaskEqStIt);
                        }
                    }
                }
            }
        }
    }
}
